(function ($) {
  var fnName = "numberic";
  jv.initMyPlugin(fnName, create);

  function create(target) {
    var $this = $(target),
      p = $this.data(fnName);

    $this.attr("type", "hidden").wrap("<div class='numberic'></div>").before("<span class='numberic-minus'></span>").before("<label>" + $this.val() + "</label>").after("<span class='numberic-add'></span>");

    var clickEvent = function (ev, direction) {
      ev.stopPropagation && ev.stopPropagation();
      var val, $inp, $label, $add, $minus, $self = $(ev.target);
      if (ev.target.tagName == "INPUT") {
        $inp = $self;
      }
      else {
        $inp = $self.siblings("input");
      }

      $label = $inp.siblings("label");
      $add = $inp.siblings(".numberic-add");
      $minus = $inp.siblings(".numberic-minus");


      val = jv.getInt($inp.val());

      if (direction == "add") {
        val += p.step;
      }
      else if (direction == "minus") {
        val -= p.step;
      }

      $inp.val(val);
      $label.html(val).showIt();

      if (direction == "add") {
        if ($inp.val() >= p.max) {
          $add.hideIt();
        }

        $minus.showIt();
      }
      else if (direction == "minus") {
        if (val <= p.min) {
          $minus.hideIt();
        }
        $add.showIt();

        if (p.hide0 && !val) {
          $label.hideIt();
        } else {
          $label.showIt();
        }
      }
      else {
        return;
      }

      if (p.changed) {
        p.changed.call($inp[0], val);
      }
    }


    $this.siblings(".numberic-minus").bind("click", function (ev) {
      clickEvent(ev, "minus");
    });

    $this.siblings(".numberic-add").bind("click", function (ev) {
      clickEvent(ev, "add");
    });


    clickEvent({target: $this});
    if (!p.min && !jv.getInt($this.val())) {
      $this.siblings(".numberic-minus").hideIt();
      $this.siblings("label").hideIt();
    }
  }


  $.fn[fnName].defaults = {
    min: 0,
    max: 100,
    step: 1,
    changed: false,
    hide0: true, // min==0 , hide0== true,  时才隐藏。
  };

  $.fn[fnName].methods = {
    options: function ($this) {
      return $this.data(fnName);
    }
  };


})($);

//--------------------------------------------------------------
